package com.base.vistter.iframe.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.vistter.iframe.constant.CommonConstant;
import com.base.vistter.iframe.entity.SysDict;
import com.base.vistter.iframe.entity.SysDictItem;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.SysDictItemMapper;
import com.base.vistter.iframe.mapper.SysDictMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
@Transactional
@Slf4j
public class SysDictItemService extends BaseService<SysDictItemMapper, SysDictItem> {

    @Resource(name = "sysDictMapper")
    private SysDictMapper sysDictMapper;

    @Transactional(readOnly = true)
    public List<SysDictItem> selectListByDictCode(String code) throws IframeException{
        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<SysDict>();
        queryWrapper.eq(SysDict::getCode, code);
        SysDict sysDict = sysDictMapper.selectOne(queryWrapper);
        if(sysDict == null){
            throw new IframeException("找不到字典标识");
        }
        LambdaQueryWrapper<SysDictItem> lambdaQueryWrapper = new LambdaQueryWrapper<SysDictItem>();
        lambdaQueryWrapper.eq(SysDictItem::getSysDictId, sysDict.getId());
        lambdaQueryWrapper.eq(SysDictItem::getStatus, CommonConstant.STATUS_NORMAL);
        lambdaQueryWrapper.orderByAsc(SysDictItem::getSort);
        return super.selectList(lambdaQueryWrapper);
    }
    public boolean save(SysDictItem entity) throws IframeException {
        return super.save(entity);
    }

    @Override
    public boolean updateById(SysDictItem entity) throws IframeException {
        return super.updateById(entity);
    }

    @Override
    public int delete(Serializable id) throws IframeException {
        return super.delete(id);
    }

    @Override
    public int delete(Collection<?> idList) throws IframeException {
        return super.delete(idList);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteByDictIds(Collection<?> idList) throws IframeException {
        LambdaQueryWrapper<SysDictItem> lambdaQueryWrapper = new LambdaQueryWrapper<SysDictItem>();
        lambdaQueryWrapper.in(SysDictItem::getSysDictId , idList);
        return super.delete(lambdaQueryWrapper);
    }

}
